//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.wsd.smartcarwasher.controller;

import com.wsd.smartcarwasher.annotation.Log;
import com.wsd.smartcarwasher.configuration.GetSessionConfig;
import com.wsd.smartcarwasher.constans.MachineEvent;
import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.domain.*;
import com.wsd.smartcarwasher.service.*;
import com.wsd.smartcarwasher.util.ReturnUtil;
import com.wsd.smartcarwasher.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.*;

@Api(
        tags = {"设备相关接口（新增）"}
)
@Controller
@CrossOrigin
public class MachineController {
    private static final Logger logger = LoggerFactory.getLogger(MachineController.class);
    @Autowired(required = false)
    private MachineService machineService;
    @Resource
    private  HttpServletRequest request;
    @Resource
    private OrderService orderService;
    @Resource
    private WxService wxService;
    @Resource
    private com.wsd.smartcarwasher.service.PriceService PriceService;
    @Resource
    private LappService lappService;
    @Resource
    private RedisService redisService;
    @Resource
    private PartnerService partnerService;

    public MachineController() {
    }

    @Log("新增设备")
    @RequiresPermissions("create:new:machine")
    @ApiOperation(
            value = "新增门店",
            notes = "门店管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParam(
            name = "machine",
            value = "门店信息",
            required = true,
            dataType = "Machine"
    )
    @RequestMapping(
            value = {"/v1/machine"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap addMachine(@RequestBody Machine machine) {
        if (machine == null) {
            return ReturnUtil.Error(ResultState.MACHINE_IS_NULL.getMsg());
        } else if (this.machineService.getMachineByName(machine.getName()) != null) {
            return ReturnUtil.Error(ResultState.DUPLICATE_MACHINE_NAME.getMsg());
        } else {
            Machine m = this.machineService.queryMachineById(machine.getId());
            if(m!=null){
                return ReturnUtil.Error("设备已存在");
            }
            int result = this.machineService.addMachine(machine);
            return result == 0 ? ReturnUtil.Error(ResultState.ADD_MACHINE_FAIL.getMsg()) : ReturnUtil.Success(ResultState.ADD_MACHINE_SUCCESS.getMsg(), machine);
        }
    }

    @ApiOperation(
            value = "新增==>根据订单号传递后端用接口方式启动设备",
            notes = "启动设备",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/start/machines"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap startMachines(@RequestBody OrderInfo orderInfo) {
        String orderSn=orderInfo.getOrderSn();
        String message=orderInfo.getMessage();
        if(orderSn.equals(""))
        {
            return ReturnUtil.Error("订单号为空");
        }
        orderInfo=this.orderService.queryOrderInfoByOrderSn(orderSn);
        if(orderInfo==null)
        {
            return ReturnUtil.Error("无该订单对应的订单信息或订单已消费");
        }
        this.machineService.startMachine(orderSn,orderInfo.getMachineId().toString());
        return  ReturnUtil.Success("成功");
    }


    @ApiOperation(
            value = "******重新设置所有机器的二维码",
            notes = "二维码",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/code/restart"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap restartMachines() {

        this.machineService.RestartGenCode();
        return  ReturnUtil.Success("成功");
    }

    @ApiOperation(
            value = "商家查询设备的运营模式",
            notes = "模式",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/h5/machine/info"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap h5MachineInfo(HttpServletRequest request,@RequestParam(value = "machineId") Integer machineId) {
String sessionId = request.getHeader("authorization");
        String openId = this.wxService.getOpenId(sessionId);
//String openId="o1MBY6MMA3D1NM_WuA8OuuwrgxrM";
        if (StringUtils.isBlank(openId)) {
            return ReturnUtil.Error(ResultState.LOGIN_AGAIN.getMsg());
        }
        Machine machine=this.machineService.getMachineByMachineId(machineId);
        //获取机器详情价格
        Map<String, Object> machineDetails=this.machineService.getMachineStatus(machineId);
        //暂停营业
        machineDetails.put("machineStatus",machine.getStatus());
        //归位状态
        if(StringUtil.isNullOrEmpty(this.redisService.get(machineId+"&restartStatus")))
        {
            this.redisService.put(machineId+"&restartStatus",false,7200L);
        }
        machineDetails.put("restartStatus",this.redisService.get(machineId+"&restartStatus"));
        //紧急停止
        if(StringUtil.isNullOrEmpty(this.redisService.get(machineId+"&stopStatus")))
        {
            this.redisService.put(machineId+"&stopStatus",false,7200L);
        }
        machineDetails.put("stopStatus",this.redisService.get(machineId+"&stopStatus"));
        //启动
        if(StringUtil.isNullOrEmpty(this.redisService.get(machineId+"&startStatus")))
        {
            this.redisService.put(machineId+"&startStatus",false,7200L);
        }
        machineDetails.put("startStatus",this.redisService.get(machineId+"&startStatus"));

        return ReturnUtil.Success("操作成功",machineDetails);
    }

    @ApiOperation(
            value = "设置模式价格",
            notes = "模式",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/h5/machine/price/update"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap h5MachinePriceUpdate(HttpServletRequest request,
                                         @RequestParam(value = "type") Integer type,
                                         @RequestParam(value = "machineId") Integer machineId,
                                         @RequestParam(value = "status") boolean status) {
String sessionId = request.getHeader("authorization");
        String openId = this.wxService.getOpenId(sessionId);
//String openId="o1MBY6MMA3D1NM_WuA8OuuwrgxrM";
        if (StringUtils.isBlank(openId)) {
            return ReturnUtil.Error(ResultState.LOGIN_AGAIN.getMsg());
        }
        Machine machine=this.machineService.getMachineByMachineId(machineId);
        Map<String, Object> machineStatus = this.machineService.getMachineStatus(machineId);
        switch (type)
        {
            case 1:machineStatus.put("quickWash",status);break;
            case 2:machineStatus.put("generalWash",status);break;
            case 3:machineStatus.put("fineWash",status);
        }
        List<String> priceList = new ArrayList<>();
        if((boolean)machineStatus.get("quickWash")==true)
        {
            priceList.add("1");
        }
        if ((boolean)machineStatus.get("generalWash")==true)
        {
            priceList.add("2");
        }
        if ((boolean)machineStatus.get("fineWash")==true)
        {
            priceList.add("3");
        }
        StringBuffer stringBuffer=new StringBuffer();
        priceList.forEach(s->stringBuffer.append(s+","));
        System.out.println(stringBuffer.toString());
        stringBuffer.deleteCharAt(stringBuffer.lastIndexOf(","));
        machine.setPriceIds(stringBuffer.toString());
        if(machine.getPriceIds().isEmpty())
        {
            machine.setPriceIds("");
        }
        this.machineService.updateMachine(machine);
        return ReturnUtil.Success("操作成功");
    }




    @ApiOperation(
            value = "暂停营业",
            notes = "暂停营业",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/h5/machine/business"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap h5MachineBusiness(HttpServletRequest request,
                                   @RequestParam(value = "machineId") Integer machineId,
                                   @RequestParam(value = "businessStatus") boolean businessStatus) {
String sessionId = request.getHeader("authorization");
        String openId = this.wxService.getOpenId(sessionId);
//String openId="o1MBY6MMA3D1NM_WuA8OuuwrgxrM";
        if (StringUtils.isBlank(openId)) {
            return ReturnUtil.Error(ResultState.LOGIN_AGAIN.getMsg());
        }
        Machine machine=this.machineService.getMachineByMachineId(machineId);
        if(businessStatus==true)
        {
            machine.setStatus(2);
        }else{
            machine.setStatus(1);
        }
        this.machineService.updateMachine(machine);
        return ReturnUtil.Success("操作成功");
    }


    @ApiOperation(
            value = "启动设备",
            notes = "启动设备",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/h5/machine/start"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap h5MachineStart(HttpServletRequest request,
                                      @RequestParam(value = "machineId") Integer machineId,
                                      @RequestParam(value = "startStatus") boolean businessStatus,
                                   @RequestParam(value = "type") Integer type) {
        String sessionId = request.getHeader("authorization");
        String openId = this.wxService.getOpenId(sessionId);
//String openId="o1MBY6MMA3D1NM_WuA8OuuwrgxrM";
        if (StringUtils.isBlank(openId)) {
            return ReturnUtil.Error(ResultState.LOGIN_AGAIN.getMsg());
        }
        Machine machine=this.machineService.getMachineByMachineId(machineId);
        OrderInfo orderInfo=new OrderInfo();
        orderInfo.setServiceId(type);
        this.machineService.startMachineH5ByPartner(machine,orderInfo);
        return ReturnUtil.Success("操作成功");
    }


    @ApiOperation(
            value = "设备复位",
            notes = "复位",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/h5/machine/reset"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap h5MachineReset(HttpServletRequest request,
                                   @RequestParam(value = "machineId") Integer machineId,
                                   @RequestParam(value = "restartStatus") boolean resetStatus) {
        String sessionId = request.getHeader("authorization");
        String openId = this.wxService.getOpenId(sessionId);
//String openId="o1MBY6MMA3D1NM_WuA8OuuwrgxrM";
        if (StringUtils.isBlank(openId)) {
            return ReturnUtil.Error(ResultState.LOGIN_AGAIN.getMsg());
        }

        Partner partner=this.partnerService.getPartnerByOpenId(openId);
        this.machineService.resetMachineH5ByPartner(machineId,partner.getId());
        return ReturnUtil.Success("操作成功");
    }

    @ApiOperation(
            value = "设备急停",
            notes = "急停",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/h5/machine/stop"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap h5MachineStop(HttpServletRequest request,
                                   @RequestParam(value = "machineId") Integer machineId,
                                   @RequestParam(value = "stopStatus") boolean stopStatus) {
String sessionId = request.getHeader("authorization");
        String openId = this.wxService.getOpenId(sessionId);
//String openId="o1MBY6MMA3D1NM_WuA8OuuwrgxrM";

        if (StringUtils.isBlank(openId)) {
            return ReturnUtil.Error(ResultState.LOGIN_AGAIN.getMsg());
        }
        Partner partner=this.partnerService.getPartnerByOpenId(openId);
        this.machineService.stopMachineH5ByPartner(machineId,partner.getId());
        return ReturnUtil.Success("操作成功");
    }






    @RequiresPermissions("update:machine")
    @ApiOperation(
            value = "根据过滤条件查询门店信息",
            notes = "门店管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParam(
            name = "filterCondition",
            value = "{'Page':1,'PageSize':20,'city':'铜川市','province':'陕西省','address':'西安曲江南'}",
            required = true,
            dataType = "Map"
    )
    @RequestMapping(
            value = {"/v1/machines"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getMachines(@RequestBody Map<String, Object> filterCondition) {
        List<Machine> ls = this.machineService.getMachines(filterCondition);
        return filterCondition == null ? ReturnUtil.Error(ResultState.FILTER_CONDITION_IS_NULL.getMsg()) : ReturnUtil.Success(ResultState.QUERY_MACHINE_SUCCESS.getMsg(),
                new Pagination(Integer.valueOf(filterCondition.get("Page").toString()),
                        Integer.valueOf(filterCondition.get("PageSize").toString()),ls)
        );
    }
    @RequiresPermissions("update:machine")
    @ApiOperation(
            value = "修改门店状态为删除",
            notes = "门店管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParam(
            name = "id",
            value = "门店id",
            required = true,
            dataType = "Integer"
    )
    @RequestMapping(
            value = {"/v1/delete_machine"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap deleteMachine(@RequestParam Integer id) {
        if (id == null) {
            return ReturnUtil.Error(ResultState.ILLEGAL_PARAM.getMsg());
        } else {
            int result = this.machineService.deleteMachine(id);
            return result == 0 ? ReturnUtil.Error(ResultState.DELETE_MACHINE_FAIL.getMsg()) : ReturnUtil.Success(ResultState.DELETE_MACHINE_SUCCESS.getMsg());
        }
    }

    @Log("修改门店")
    @RequiresPermissions("update:machine")
    @ApiOperation(
            value = "更新门店信息",
            notes = "门店管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParam(
            name = "machine",
            value = "门店信息",
            required = true,
            dataType = "Machine"
    )
    @RequestMapping(
            value = {"/v1/update_machine"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap updateMachine(@RequestBody Machine machine) {
        if (machine == null) {
            return ReturnUtil.Error(ResultState.MACHINE_IS_NULL.getMsg());
        } else {
            int result = this.machineService.updateMachine(machine);
            if (result == 0) {
                logger.info("update machine fail", machine);
                return ReturnUtil.Error(ResultState.UPDATE_MACHINE_FAIL.getMsg());
            } else {
                return ReturnUtil.Success(ResultState.UPDATE_MACHINE_SUCCESS.getMsg());
            }
        }
    }

    @ApiOperation(
            value = "根据门店id查询门店信息",
            notes = "门店管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParam(
            name = "id",
            value = "门店id",
            required = true,
            dataType = "Integer"
    )
    @RequestMapping(
            value = {"/v1/machine"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getMachineById(@RequestParam Integer id) {
        if (id == null) {
            return ReturnUtil.Error(ResultState.QUERY_PARAM_IS_EMPTY.getMsg());
        } else {
            Machine machine = this.machineService.queryMachineById(id);
            return machine == null ? ReturnUtil.Error(ResultState.EXIST_NO_MACHINE.getMsg()) : ReturnUtil.Success(ResultState.QUERY_SUCCESS.getMsg(), machine);
        }
    }

    //=================== new lyj  ============
    @ApiOperation(
            value = "查询所有门店信息",
            notes = "门店管理",
            produces = "application/json",
            response = ModelMap.class
    )
//    @RequiresPermissions("query:all:machines")
    @ApiImplicitParam(  name = "id", value = "门店id",  required = true, dataType = "Integer" )
    @RequestMapping(value = {"/v1/machines"},  method = {RequestMethod.GET}, produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ModelMap getMachines() {
        return ReturnUtil.Success(ResultState.QUERY_SUCCESS.getMsg(), this.machineService.getAllMachine());
    }

    @ApiOperation(value = "用于小程序查询所有门店信息带距离显示",notes = "门店管理",produces = "application/json",response = ModelMap.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Lng", value = "经度", required = true, dataType = "String"),
            @ApiImplicitParam(name = "Lat", value = "维度", required = true, dataType = "String")
    })
    @RequestMapping(value = {"/v1/wx_machines"},method = {RequestMethod.GET}, produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ModelMap getWXMachines(@RequestParam("Lng")String Lng,@RequestParam("Lat")String Lat ) {
        //return ReturnUtil.Success(ResultState.QUERY_SUCCESS.getMsg(), this.machineService.getAllMachine());
        return ReturnUtil.Success(ResultState.QUERY_SUCCESS.getMsg(), this.machineService.getAllMachine(Lng,Lat));
    }

    @ApiOperation(value = "(新增=>小程序首页)小程序展示附近的多少公里内的服务点，按距离排列",notes = "门店管理",produces = "application/json",response = ModelMap.class)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "Lng", value = "经度", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "Lat", value = "维度", required = true, dataType = "String")
//    })
//    @RequiresPermissions("query:all:machines")
    @RequestMapping(value = {"/v1/wx_nearby_machines"},method = {RequestMethod.GET}, produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ModelMap getNearByWXMachines(@RequestParam("Lng")String Lng, @RequestParam("Lat")String Lat, HttpServletRequest request) throws UnsupportedEncodingException {
        return ReturnUtil.Success(ResultState.QUERY_SUCCESS.getMsg(), this.machineService.getNearByMachine(Lng,Lat,request));
    }

    @ApiOperation(  value = "根据门店id查询门店信息",  notes = "门店管理",  produces = "application/json", response = ModelMap.class)
    //@ApiImplicitParam( name = "id", value = "门店id", required = true,  dataType = "Integer" )
    @RequestMapping( value = {"/v1/machine/detail"}, method = {RequestMethod.GET},  produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ModelMap getMachineDetailById(@RequestParam Integer machineId) {
        if (machineId == null) {
            return ReturnUtil.Error(ResultState.QUERY_PARAM_IS_EMPTY.getMsg());
        } else {
            Machine machine = this.machineService.queryMachineById(machineId);
            Map<String,Object> map=new HashMap<>();
            map.put("machine",machine);
            map.put("price",this.PriceService.getMachinePrice(machineId));
            return machine == null ? ReturnUtil.Error(ResultState.EXIST_NO_MACHINE.getMsg()) : ReturnUtil.Success(ResultState.QUERY_SUCCESS.getMsg(), map);
        }
    }

//    @ApiOperation(  value = "获取门店排队视频图片",  notes = "门店管理",  produces = "application/json", response = ModelMap.class)
//    //@ApiImplicitParam( name = "id", value = "门店id", required = true,  dataType = "Integer" )
//    @RequestMapping( value = {"/v1/machine/queue_up"}, method = {RequestMethod.GET},  produces = {"application/json; charset=utf-8"})
//    @ResponseBody
//    public ModelMap queue_up(@RequestParam Integer machineId) {
//        if (machineId == null) {
//            return ReturnUtil.Error(ResultState.QUERY_PARAM_IS_EMPTY.getMsg());
//        } else {
//            String machine = this.machineService.queue_up(machineId);
//            return machine == null ? ReturnUtil.Error("暂时没有图片") : ReturnUtil.Success(ResultState.QUERY_SUCCESS.getMsg(), machine);
//        }
//    }

    @ApiOperation(  value = "获取门店排队视频图片",  notes = "门店管理",  produces = "application/json", response = ModelMap.class)
    //@ApiImplicitParam( name = "id", value = "门店id", required = true,  dataType = "Integer" )
    @RequestMapping( value = {"/v1/machine/queue_up"}, method = {RequestMethod.GET},  produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ModelMap queue_up(@RequestParam Integer machineId) {
        if (machineId == null) {
            return ReturnUtil.Error(ResultState.QUERY_PARAM_IS_EMPTY.getMsg());
        } else {
//            String machine = this.machineService.queue_up(machineId);
            Machine machine=this.machineService.getMachineByMachineId(machineId);
            if(!StringUtil.isNullOrEmpty(machine))
            {
                if(StringUtil.isNullOrEmpty(machine.getCameraCode()))
                {
                    return ReturnUtil.Success(ResultState.QUERY_SUCCESS.getMsg(), "https://luobotuchuang.ytby0402.top/washWaringImg.jpg");
                }else{
                    Integer cameraPosition=1;
                    if(!StringUtil.isNullOrEmpty(machine.getCameraPosition()))
                    {
                        cameraPosition=machine.getCameraPosition();
                    }
                    String picUrl=this.lappService.getCapture(machine.getCameraCode(),cameraPosition);
                    return ReturnUtil.Success(ResultState.QUERY_SUCCESS.getMsg(), picUrl);
                }
            }else{
                return ReturnUtil.Success(ResultState.QUERY_SUCCESS.getMsg(), "https://luobotuchuang.ytby0402.top/washWaringImg.jpg");
            }
        }
    }

    @ApiOperation(  value = "检查车辆是否停到位置了 ",  notes = "检查车辆是否停到位置了",  produces = "application/json", response = ModelMap.class)
    @RequestMapping( value = {"/v1/machine/carStopIsOk"}, method = {RequestMethod.GET},  produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ModelMap carStopIsOk(@RequestParam Integer machineId) {
        String sessionId = request.getHeader("authorization");
        UserInfo user = this.wxService.getUserInfoBySessionId(sessionId);
        if (user == null) {
            return ReturnUtil.Error(ResultState.GET_USER_INFO_FAILED.getMsg());
        }
        MqttService.carIsStop.remove("carStopIsOk_" + machineId);
        if (machineId == null) {
            return ReturnUtil.Error(ResultState.QUERY_PARAM_IS_EMPTY.getMsg());
        } else {
            String machine = this.machineService.carStopIsOk(machineId);
            //检查该车辆是否是运营状态
            if(this.machineService.getMachineByMachineId(machineId).getIs_operate()==0)
            {
                return ReturnUtil.Error("设备不在运营状态");
            }
            if(this.machineService.getMachineByMachineId(machineId).getWorkState()==101)
            {
                return ReturnUtil.Error("设备不在运营状态");
            }

            return machine == null ? ReturnUtil.Error("设备没有返回状态") : ReturnUtil.Success(ResultState.QUERY_SUCCESS.getMsg(), machine);
        }
    }
    //==========================================================
    @ApiOperation(
            value = "根据门店名称查询门店信息",
            notes = "门店管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParam(
            name = "name",
            value = "门店名称",
            required = true,
            dataType = "String"
    )
    @RequestMapping(
            value = {"/v1/machine_by_name"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getMachineByName(@RequestParam String name) {
        return ReturnUtil.Success(ResultState.QUERY_SUCCESS.getMsg(), this.machineService.getMachineByName(name));
    }

    @ApiOperation(
            value = "查询机器通信消息事件",
            notes = "门店消息事件",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/all_machine_event"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getAllMachineEvent() {
        return ReturnUtil.Success(ResultState.QUERY_MACHINE_EVENT_SUCCESS.getMsg(), MachineEvent.allEvent().toList());
    }

    @ApiOperation(
            value = "生成指定机器的小程序码",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParam(
            name = "machineId",
            value = "机器id",
            required = true,
            dataType = "Integer"
    )
    @RequestMapping(
            value = {"/v1/generate_machine_wx_code"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap generateMachineWxCode(@RequestParam("machineId") int machineId) {
        Machine machine = this.machineService.queryMachineById(machineId);
        if (machine == null) {
            return ReturnUtil.Error(ResultState.EXIST_NO_MACHINE.getMsg());
        } else {
            String result = this.machineService.generateWxCode(machine);
            return result == null ? ReturnUtil.Error(ResultState.FAIL_GENERATE_WX_CODE.getMsg()) : ReturnUtil.Success(ResultState.SUCCESS_GENERATE_WX_CODE.getMsg(), result);
        }
    }

    @ApiOperation(
            value = "查询机器当前是否可以使用",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParam(
            name = "machineId",
            value = "机器id",
            required = true,
            dataType = "Integer"
    )
    @RequestMapping(
            value = {"/v1/machine_status"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap canMachineUsed(@RequestParam("machineId") int machineId) {
        Machine machine = this.machineService.queryMachineById(machineId);
        if (machine == null) {
            return ReturnUtil.Errorone(ResultState.EXIST_NO_MACHINE.getMsg(),null);
        }
//        else if (machine.getStatus() == 2) {
//            return ReturnUtil.Errorone(ResultState.MACHINE_STATUS_CLOSED.getMsg(),machine.getName());
//        }
        if (machine.getStatus() == 0) {
            return ReturnUtil.Errorone(ResultState.MACHINE_STATUS_REMOVED.getMsg(),machine.getName());
        }
        else if (machine.getWorkState() == 101) {
            machine.setWorkState(102);
            this.machineService.updateMachine(machine);
            return machine.getOnline() == 0 ? ReturnUtil.Errorone(ResultState.MACHINE_OFFLINE.getMsg(),machine.getName()) : ReturnUtil.SuccessMachineName(ResultState.MACHINE_CAN_USED.getMsg(),machine.getName());
//            return ReturnUtil.Errorone(ResultState.MACHINE_WORK_STATE_OCCUPIED.getMsg(),machine.getName());
        } else {
            return machine.getOnline() == 0 ? ReturnUtil.Errorone(ResultState.MACHINE_OFFLINE.getMsg(),machine.getName()) : ReturnUtil.SuccessMachineName(ResultState.MACHINE_CAN_USED.getMsg(),machine.getName());
        }
    }

    @ApiOperation(
            value = "查询未关联运营商的门店",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/unrelated_machines"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap unrelatedMachines() {
        return ReturnUtil.Success(ResultState.QUERY_MACHINE_SUCCESS.getMsg(), this.machineService.queryUnRelatedMachines());
    }

    @ApiOperation(
            value = "重置指定机器状态，营业，空闲，离线状态",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/reset_machine"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap resetMachine(@RequestParam int machineId) {
        boolean isSuccess = this.machineService.resetMachine(machineId);
        if (!isSuccess) {
            ReturnUtil.Error(ResultState.RESET_MACHINE_FAIL.getMsg());
        }

        return ReturnUtil.Success(ResultState.RESET_MACHINE_SUCCESS.getMsg());
    }


    @Log("修改了设备状态")
    @RequiresPermissions("update:machine")
    @ApiOperation(
            value = "PC-更新门店状态",
            notes = "门店管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParam(
            name = "machine",
            value = "{'id':'51','online':'0- 离线 1-在线','status':'1 正常服务  2 暂停服务'}",
            required = true,
            dataType = "Machine"
    )
    @RequestMapping(
            value = {"/v1/update_machine_status"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap updateMachineStatus(@RequestBody Machine machine) {
        return this.machineService.updateMachineStatus(machine);
    }



//    @Log("修改了总开关状态")
//    @RequiresPermissions("update:machine")
//    @ApiOperation(
//            value = "【8.14 new】PC-更新设备总开关状态",
//            notes = "门店管理",
//            produces = "application/json",
//            response = ModelMap.class
//    )
//    @ApiImplicitParam(
//            name = "machine",
//            value = "{'id':'51','online':'0- 离线 1-在线'}",
//            required = true,
//            dataType = "Machine"
//    )
//    @RequestMapping(
//            value = {"/v1/update_machine_online"},
//            method = {RequestMethod.POST},
//            produces = {"application/json; charset=utf-8"}
//    )
//    @ResponseBody
//    public ModelMap updateMachineOnline(@RequestBody Machine machine) {
//        return this.machineService.updateMachineOnline(machine);
//    }






    @ApiOperation(
            value = "pc-获取设备列表信息",
            notes = "门店管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/machines/getMachinesByList"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getMachinesByList(@RequestParam Integer Page,@RequestParam Integer PageSize,
                                      @RequestParam(required = false) String  address,
                                      @RequestParam(required = false) String  name,
                                      @RequestParam(required = false) Integer  id,
                                      @RequestParam(required = false) Integer  partnerId,
                                      @RequestParam(required = false) Integer  storeId
    ) {
        Map<String, Object> filterCondition = new HashMap<>();
        filterCondition.put("address",address);
        filterCondition.put("name",name);
        filterCondition.put("id",id);
        filterCondition.put("partnerId",partnerId);
        filterCondition.put("storeId",storeId);
        List<Machine> ls = this.machineService.getMachines(filterCondition);
        return filterCondition == null ? ReturnUtil.Error(ResultState.FILTER_CONDITION_IS_NULL.getMsg()) : ReturnUtil.Success(ResultState.QUERY_MACHINE_SUCCESS.getMsg(),
                new Pagination(Page, PageSize,ls)
        );
    }

    @ApiOperation(
            value = "商家端-获取设备列表信息",
            notes = "设备管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/store/machine/getMachinesByList"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getMachinesByList(@RequestParam Integer Page,@RequestParam Integer PageSize,
                                      @RequestParam(required = false) String  address,
                                      @RequestParam(required = false) String  name,
                                      @RequestParam(required = false) Integer  id,
                                      @RequestParam(required = false) Integer  storeId
    ) {
        GetSessionConfig config=new GetSessionConfig();
        Map<String,String> map= config.getStorePartnerId("获取设备列表信息",request);
        Map<String, Object> filterCondition = new HashMap<>();
        filterCondition.put("address",address);
        filterCondition.put("name",name);
        filterCondition.put("id",id);
        filterCondition.put("storeId",storeId);
        filterCondition.put("partnerId",map.get("partnerId"));
        List<Machine> ls = this.machineService.getMachines(filterCondition);
        return filterCondition == null ? ReturnUtil.Error(ResultState.FILTER_CONDITION_IS_NULL.getMsg()) : ReturnUtil.Success(ResultState.QUERY_MACHINE_SUCCESS.getMsg(),
                new Pagination(Page, PageSize,ls)
        );
    }
    @ApiOperation(
            value = "小程序-获取优惠卷下面的设备",
            notes = "门店管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/machines/getCouponMachines"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getCouponMachines(@RequestParam Integer Page,@RequestParam Integer PageSize,
                                      @RequestParam(required = false) Integer  id

    ) {
        List<Machine> ls = this.machineService.getCouponMachines(id);
        return ReturnUtil.Success(ResultState.QUERY_MACHINE_SUCCESS.getMsg(),new Pagination(Page, PageSize,ls));
    }

    @ApiOperation(
            value = "【new】更新设备是否是运营状态",
            notes = "设备运营",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/machines/setOperate"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap setMachineOperate(@RequestParam(value = "is_operate") Integer  is_operate,
                                      @RequestParam(value = "machineId") String  machineId) {
        int changes=this.machineService.setMachineOperate(is_operate,machineId);
        if(changes!=0)
        {
            return ReturnUtil.Success("开关总状态修改成功");
        }
        return ReturnUtil.Error("开关总状态修改失败");
    }



}
